const app = getApp()
Page({
    data: {
        config: app.config,
        shops:{},

        page:1,
        totalPage: '',

        topBannarColor: 0,
        titleBarHeight: 80,

        shopData: {},

        goodsList: []
    },
    onLoad(query) {

        let shops = JSON.parse(query.param)
		console.log(shops)
        shops.image = shops.image && JSON.parse(shops.image)
        this.setData({
            shops: shops
        })
        this.setBannarHei();

        this.getShop();
        this.getGoodsList();
    },


    getGoodsList(){
      
      let dat = {
        sortType: 1,
        sid: this.data.shops.id,
        // sid: 56,
      }

      app.product.findAllProduct(dat)
      .then(res=>{

        this.dataPromise(res.data.list)
        .then(dat=>{
          this.setData({
            goodsList: dat
          })
        })
      })

    },


    setBannarHei(){
      my.getSystemInfo({
        success: (res) => {
          let heigh = res.statusBarHeight + res.titleBarHeight;
          this.setData({
            titleBarHeight: heigh
          })
        }
      })
    },

    onPageScroll({ scrollTop }) {
      let ratio = Math.floor(scrollTop/6);
      let opention = ratio*0.05;
      this.setData({
        topBannarColor: opention
      })
    },


    getShop(){
      let id = this.data.shops.id;
      app.product.shops(id)
      .then(res=>{
        if(res.data.image){
          res.data.image = JSON.parse(res.data.image);
        }
        if(res.data.headPortrait){
          res.data.headPortrait = JSON.parse(res.data.headPortrait);
        }
        this.setData({
          shopData: res.data
        })
      })
    },

    // 跳转
    navigateTo(e){
        app.navigateTo(e)
    },

    // 转价格
    dataPromise(list) {
        return new Promise((relose, reject) => {
            list.forEach((item) => {
                item.category = JSON.parse(item.category)
                item.colors = JSON.parse(item.colors)
                item.match = JSON.parse(item.match)
                item.params = JSON.parse(item.params)
                item.slideshows = JSON.parse(item.slideshows)
                item.img = item.slideshows[0]
                item.title = item.tit
                this.getminPrice(item.params)
                    .then(res => {
                        item.price = res
                    })
            })
            relose(list)
        })
    },

    getminPrice(params) {
        return new Promise((resole, reject) => {
            let prices = []
            for (let i = 0; i < params.length; i++) {
                for (let t = 0; t < params[i].term_list.length; t++) {
                    for (let s = 0; s < params[i].term_list[t].specifications.length; s++) {
                        let price = (params[i].term_list[t].specifications[s].money / params[i].term_list[t].value / 100).toFixed(2)
                        prices.push(price)
                    }
                }
            }
            let minprice = Math.min.apply(null, prices);
            resole(minprice)
        })
    },



   //   下拉加载
    onReachBottom() {
        console.log("11111111111")
        let that = this
        // 判断是否还有数据需要加载
        if (that.data.page < that.data.totalPage) {
            that.setData({ show: true });
            const newPage = that.data.page + 1;
            that.setData({ page: newPage });
        } else {
            that.setData({ show: false });
        }
    },
});
